<?php
/**
 * File: Cart.php
 * User: Mr.Yang
 * Date: 2025/3/26 0026
 * Time: 14:06
 * Note:
 */

namespace app\home\controller;

use app\admin\model\Address;
use app\admin\model\Book as BookModel;
use app\admin\model\Cart as CartModel;
use app\admin\model\User as UserModel;

class Cart extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }

    /**
     * Note: 购物车列表
     * User: Mr.Yang
     * @return mixed
     */
    public function index()
    {
        $uid = session('userid');
        //获取购物车列表
        $cart = CartModel::field('c.*, b.bname, b.thumb, b.price')
            ->alias('c')
            ->leftJoin('book b', 'b.id=c.bookid')
            ->where('c.userid', '=', $uid)
            ->order('c.id', 'desc')
            ->select();
        $this->assign('cart', $cart);
        return $this->fetch();
    }

    /**
     * Note: 加入购物车
     * User: Mr.Yang
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $bookid = $this->request->post('bookid', 0);
            $book = (new BookModel())->getOneById($bookid);
            if ($book['status'] == 0) {
                $this->error('图书已下架');
            }
            if ($book['total'] < 1) {
                $this->error('图书库存不足');
            }
            $uid = session('userid');
            //判断是否已在购物车
            $count = CartModel::where([['bookid', '=', $bookid], ['userid', '=', $uid]])->count();
            if ($count > 0) {
                $this->error('图书已在购物车中');
            }
            $res = CartModel::create([
                'bookid' => $bookid,
                'userid' => $uid,
                'number' => 1,
                'date_entered' => date('Y-m-d H:i:s')
            ]);
            if ($res) {
                //获取购物车数量
                $count = CartModel::where([['userid', '=', $uid]])->count();
                $this->success('加入购物车成功', '', $count);
            }
            $this->error('加入购物车失败');
        }
    }

    /**
     * Note: 删除购物车中的商品
     * User: Mr.Yang
     * @throws \Exception
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->get('id', 0);
            $uid = session('userid');
            $count = CartModel::where([['id', '=', $id], ['userid', '=', $uid]])->count();
            if ($count < 1) {
                $this->error('购物车中没有该商品');
            }
            $res = CartModel::where('id', '=', $id)->delete();
            if ($res) {
                $this->success('删除商品成功');
            }
            $this->error('删除商品失败');
        }
    }

    /**
     * Note: 订单检查界面
     * User: Mr.Yang
     * @return mixed
     */
    public function checkout()
    {
        $cartid = $this->request->get('cartid');
        if (!$cartid) {
            $this->redirect(url('Cart/index'));
        }
        //判断是否有地址
        $uid = session('userid');
        $address = Address::where('userid', '=', $uid)->select()->toArray();
        if (!$address) {
            $this->redirect(url('User/address'));
        }

        //获取选中的购物车商品列表
        list($cart, $total) = $this->getCartAndTotal($uid, $cartid);

        foreach ($address as $k => $v) {
            $address[$k]['address'] = $this->area_pos($v['areaid'], '').$v['address'];
        }

        //获取我的金额
        $user = UserModel::where('id', '=', $uid)->findOrFail();

        $this->assign('total', $total);
        $this->assign('cart', $cart);
        $this->assign('address', $address);
        $this->assign('cartid', $cartid);
        $this->assign('user', $user);
        return $this->fetch();
    }
}